// 1. 使用单独的文件管理type

import { TODO_ADD, TODO_DEL_BY_ID, TODO_UPDATE } from "./actionType";

/*

      学习目标：使用Redux-Thunk处理异步Action
      1.
    */
function loadDataAPI(params) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("2秒后返回数据  ----->  ", 2);
      resolve({
        task: "React学完了",
        isDone: false,
        id: Date.now(),
      });
    }, 2000);
  });
}

// 🔔Redux 不能处理异步Action
// 1. 下包： redux-thunk中间件：处理异步行为
// 2. 配置中间件redux-thunk
// 💥 3.1.Action创建函数，返回一个(异步)函数
// 💥 3.2.从返回函数的形参中，拿到dispatch，使用dispatch发起action

// todos

export const updateActionCreator = (id) => {
  return {
    type: TODO_UPDATE,
    payload: id,
  };
};

export const dltActionCreator = (id) => {
  return {
    type: TODO_DEL_BY_ID,
    payload: id,
  };
};

export const addActionCreator = (newTodo) => {
  return async (dispatch) => {
    const res = await loadDataAPI();
    console.log("dispatch  ----->  ", dispatch);
    dispatch({
      type: TODO_ADD,
      payload: res,
    });
  };
};
